<%@ page import="java.util.*,com.apexedu.util.*,com.apexedu.portal.util.*" contentType="text/html;charset=UTF-8"
         language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
    String[] weekdays = {"日", "一", "二", "三", "四", "五", "六"};
    DBMUtil dbutil = new DBMUtil("oa");
    boolean isExist = true;
    StringBuffer sb = new StringBuffer("");
    String zbh = request.getParameter("zbh");
    String id = StringUtil.EMPTY;
    String sql = "select zc.id,x.xqmc,to_char(zc.mc) as mc, ksrq, jsrq,zc.zbh as zbh,x.id as xqid from t_zc zc,t_xq x" +
            " where zc.xq = x.id and to_number(to_char(sysdate,'yyyyMMdd')) between zc.ksrq and zc.jsrq";
    List<Map<String, Object>> listZc = dbutil.queryForList(null, sql);
    if (listZc.isEmpty()) {
        isExist = false;
    } else {
        id = listZc.get(0).get("id").toString();
        request.setAttribute("zcid", id); //当前周次id
        request.setAttribute("thiszbh", listZc.get(0).get("zbh").toString()); //当前周次id
        String xqid = listZc.get(0).get("xqid").toString();
        request.setAttribute("xqid", xqid);

        List<Map<String, Object>> maxZbh = dbutil.queryForList(null, "select max(zbh) as zbh from t_zc zc where zc.xq=?", xqid);
        request.setAttribute("maxZbh", maxZbh.get(0).get("zbh").toString());

        if (StringUtil.isNotBlank(zbh) && !zbh.equals(listZc.get(0).get("zbh").toString())) {
            listZc = dbutil.queryForList(null, "select zc.id,x.xqmc,to_char(zc.mc) as mc, ksrq, jsrq,zc.zbh as zbh,x.id as xqid from t_zc zc,t_xq x" +
                    " where zc.xq = x.id and zc.zbh=? and zc.xq=?", zbh, xqid);

        }
        request.setAttribute("zbh", listZc.get(0).get("zbh").toString());
        id = listZc.get(0).get("id").toString();
        request.setAttribute("nzcid", id);
        String mc = listZc.get(0).get("mc").toString();// 名称
        String xqmc = listZc.get(0).get("xqmc").toString(); // 学期名称

        sql = "select ap.week, ap.syrq as rq, case when ap.aplx=4 then ap.mc else to_char('<center><h3>'|| ap.mc|| '</h3></center>"
                + " <div style=''margin-left: 60px;''><span>会议时间：<label>'||case when lxtz=1 then '另行通知' else to_char(ap.syrq,'hh24:mi') || '-' || to_char(ap.syrq2,'hh24:mi')  end ||'</label></span><br>"
                + " <span>会议地点：<label>' || (select wm_concat(hys.hytmc) from T_GWHDGL_HYTGL hys where hys.id in (select column_value from  table(split(ap.sqhycs))))  || '</label></span><br>"
                + " <span>出席人员：<label>' || ap.CJRY ||  "
                + " '</label></span><br><span>责任部门：<label>' ||(select wm_concat(b.NAME) from lborganization b where b.ID in  (select * from table(split(ap.sbbm,';'))))|| '</label></span></div>') end as nr  "
                + " from T_GWHDGL_HYAPB ap  where ap.zc = ? order by week,ap.syrq,ap.id ";

        List<Map<String, Object>> list = dbutil.queryForList(null, sql, id);

        Calendar cal = Calendar.getInstance();
        // 赋值时间 字符型DateUtil.PATTERN_DATE
        // 赋值时间 number
        cal.setTime(DateUtil.parseDate(listZc.get(0).get("ksrq").toString(), "yyyyMMdd"));

        sb.append("<tr><th colspan=4 class='titleh'>").append("上海工会管理职业学院" + xqmc + "<br>" + mc + "工作安排（代通知)").append("</th></tr>");
        sb.append("<tr class='title'><th rowspan=2 class='title' style='width:10%;'>").append("日期").append("</th>");
        sb.append("<th rowspan=2 style='width:10%;'>").append("周次").append("</th>");
        sb.append("<th colspan=2>").append("工作安排").append("</th>");
        sb.append("</tr>");
        sb.append("<tr class='title'>");
        sb.append("<th style='width:40%;'>").append("上午").append("</th>");
        sb.append("<th style='width:40%;'>").append("下午").append("</th>");
        sb.append("</tr>");
        for (int i = 0; i < 7; i++) {
            // 获取时间
            sb.append("<tr><td>").append(DateUtil.formatDate(cal.getTime(), "MM/dd")).append("</td>");
            // 获取周次
            sb.append("<td style='text-align:center';>").append(weekdays[cal.get(Calendar.DAY_OF_WEEK)-1]).append("</td>");

            sb.append("<td class='content' valign='middle'>");
            for (Map<String, Object> map : list) {
                String rq = map.get("rq").toString();
                // 判断上午 yyyy-MM-dd hh24:mi:ss substring(11, 13)hh
                if (rq.substring(0, 10).equals(DateUtil.formatDate(cal.getTime(), DateUtil.PATTERN_DATE))) {
                    if (Integer.parseInt(rq.substring(11, 13)) < 12) {
                        sb.append(map.get("nr"));
                    }
                }
            }
            sb.append("</td>");
            // 判断下午
            sb.append("<td class='content' valign='middle'>");
            for (Map<String, Object> map : list) {
                String rq = map.get("rq").toString();
                if (rq.substring(0, 10).equals(DateUtil.formatDate(cal.getTime(), DateUtil.PATTERN_DATE))) {
                    if (Integer.parseInt(rq.substring(11, 13)) >= 12) {
                        sb.append(map.get("nr"));
                    }
                }
            }
            sb.append("</td></tr>");
            cal.add(Calendar.DATE, 1);
        }
    }
%>
<html>
<head>
    <title>上海工会管理职业学院</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style>
        h2 {
            text-align: center
        }

        .yzhytable {
            margin: 0 auto;
            font-size: 18px;
            border-collapse: collapse;
            border: 1px solid #333;
        }
        .yzhytable td,.yzhytable th{
            min-height: 36px;
            border: 1px solid #333;
        }

        .yzhytable td h3 {
            margin: 10px auto;
        }
        .yzhytable td.sleep {
            line-height: 36px;
            text-align: center;
            font-size: 18px;
            font-weight: bolder;
        }

        .yzhytable td div span {
            text-align: left;
            display: inline-block;
            width: 100%;
        }

        .yzhytable td div span label{
            display: inline-block;
            width: 200px;
            vertical-align: top;
        }

        .yzhytable .content {
            min-height: 36px;
            height: 36px;
            width: 400px;
            vertical-align: top;
            padding-bottom: 5px;
        }

        .yzhytable .titleh {
            font-size: 24px;
            text-align: center;
        }

        .yzhytable .title {
            text-align: center;
        }

        .yzhytable .content .fonttitle {
            font-family: Verdana, Geneva, sans-serif;
            font-weight: bold;
        }

        .yzhytable .content .message {

        }

        .print, .week {
            line-height: 36px;
            display: inline-block;
            cursor: pointer;
            color: blue;
            text-align: right;
            padding: 0 10px;
            margin: 0 10px;
        }
    </style>
</head>
<body>

<div id="main">
    <jsp:include page="head.jsp"/>
    <script>
        $(function () {
            $(".prevWeek").click(function () {
                if (parseInt($(":hidden[name='zbh']").val(), 10) == 1) {
                    return false;
                }
                $(":hidden[name='zbh']").val(function () {
                    return parseInt($(this).val(), 10) - 1;
                });
                document.forms[0].submit();
            });
            $(".nextWeek").click(function () {
                if ($(":hidden[name='zbh']").val() == $("#maxZbh").val()) {
                    return false;
                }
                $(":hidden[name='zbh']").val(function () {
                    return parseInt($(this).val(), 10) + 1;
                });
                document.forms[0].submit();
            });
            $(".thisWeek").click(function () {
                $(":hidden[name='zbh']").val($("#thiszbh").val());
                document.forms[0].submit();
            });

            $("#yzhytable").find("tr").each(function(){
//                if($(this).find("td:eq(1)").is(":contains('六')") || $(this).find("td:eq(1)").is(":contains('日')")){
//                    if($(this).find("td:eq(2)").is(":empty") && $(this).find("td:eq(3)").is(":empty")){
////                        $(this).find("td:eq(3)").remove();
////                        $(this).find("td:eq(2)").attr("colspan","2").addClass("sleep").html("休&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;息");
//                        var td2 = $(this).find("td:eq(1)").html();
//                        var html = getHtml($(this).find("td:eq(0)")) + "<td>"+td2+"</td><td class='sleep' colspan='2'>休&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;息</td>";
//                        $(this).html(html);
//                    }
//                }
				if($(this).find("td:eq(2)").is(":not(:contains('会议时间'))")
                        &&　
                ($(this).find("td:eq(3)").is(":not(:contains('会议时间'))") || $(this).find("td:eq(3)").is(":empty"))){
					var td2 = $(this).find("td:eq(1)").html();
					var html = getHtml($(this).find("td:eq(0)")) + "<td>"+td2+"</td><td class='sleep' colspan='2'>"+$(this).find("td:eq(2)").html()+"</td>";
					$(this).html(html);
				}
            });
        });

        function getHtml(obj){
            obj.wrap("<div id='innerDiv'></div>");
            var html_ = $("#innerDiv").html();
            $(this).unwrap();
            return html_;
        }
    </script>
    <form>
        <input type="hidden" name="thiszbh" id="thiszbh" value="${thiszbh}"/>
        <input type="hidden" name="xqid" value="${xqid}"/>
        <input type="hidden" name="zbh" value="${zbh}"/>
        <input type="hidden" name="maxZbh" id="maxZbh" value="${maxZbh}"/>
    </form>

    <div id="content">
        <div id="contentSub" style="min-height: 330px; height: auto !important;">
            <div id="contentTitleDiv">
                <%if (!isExist) {%>
                <div style="line-height: 100px;font-size: 36px;color: red;">
                    一周会议无法获取周次
                </div>
                <%} else {%>
                <div style="text-align: right;">
                    <span class="prevWeek week">上一周</span>
                    <span class="thisWeek week">本周</span>
                    <span class="nextWeek week">下一周</span>
                    <span class="print"
                          onclick="window.open('http://rep.shghxy.com:8792/s/report/1?zc=${nzcid}')">打印</span>
                </div>
                <table cellpadding="0PX" cellspacing="0PX" border="1PX"
                       class="yzhytable" id="yzhytable">
                    <%=sb.toString()%>
                </table>
                <%}%>
            </div>
        </div>
        <div id="contentBg"></div>
    </div>
    <jsp:include page="foot.jsp"/>
</div>
</body>
</html>
